/* File makes Table 13 Panel A and Table 1 panel C */

/* 	Input files: 	fw_info_silo, permno_symbol_match_final, ravenpack_data_final, crsp1019_ret_asvi,
	Output files: stock_day, T13a_volume.tex, T1c_sum_stat_panelC.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************



use fw_info_silo, clear 
gduplicates drop
foreach var of varlist _all {
	rename `var' `=lower("`var'")'   // "
}

ren std_orig_signals s_disagree
ren std_rec_mean_signal r_disagree
ren mean_rec_signal_std r_uncertainty

drop if mi(r_disagree) | mi(s_disagree) | mi(r_uncertainty) 


tsset symbolid date
forval k=1/5 {
gen nsig_l`k'=l`k'.num_orig_signals
replace nsig_l`k'=0 if mi(nsig_l`k')
}
g nsignals_l5day=nsig_l1 + nsig_l2 + nsig_l3 + nsig_l4 + nsig_l5
	drop nsig_l*


merge m:1 symbolid using permno_symbol_match_final
g year=year(date)
g month=month(date)
g ym=ym(year,month)
format ym %tm
tab ym _merge
keep if _merge>=3
drop _merge year month

merge 1:1 date permno using ravenpack_data_final, update
tab ym _merge
drop if _merge==2
drop _merge 
	foreach k in n_articles n_wsj {  
	replace `k'=0 if mi(`k')
	}
	
gen media=cond(n_articles>0,1,0) 


merge 1:1 date permno using crsp1019_ret_asvi, keep(match) nogen
gsort date permno
replace asvi=0 if mi(asvi)  

gen mbin0=cond(num_orig_signals==0,1,0)
gen mbin1=cond(num_orig_signals==1,1,0)
gen mbin2=cond(num_orig_signals==2,1,0)
gen mbin3=cond(num_orig_signals==3,1,0)
gen mbin4=cond(num_orig_signals==4,1,0)
gen mbin5_10=cond(inrange(num_orig_signals,5,10),1,0)
gen mbin11_30=cond(inrange(num_orig_signals,11,30),1,0)
gen mbin30p=cond(inrange(num_orig_signals,31,10000000000000),1,0)



drop if mi(ablogvol) | mi(ablogvol_l1) | mi(sdabret_l1to5) 


qui sum s_disagree 
	gen sdev_s_disagree=r(sd) 
	gen avg_s_disagree=r(mean)
gen std_orig_signals=(s_disagree-r(mean))/r(sd)

qui sum r_disagree 
	gen sdev_r_disagree=r(sd) 
	gen avg_r_disagree=r(mean)
gen std_rec_mean_signal=(r_disagree-r(mean))/r(sd)

qui sum r_uncertainty 
gen mean_rec_signal_std=(r_uncertainty-r(mean))/r(sd)


drop permco ticker_crsp name_crsp n_articles n_wsj mktdate symbol_ticker tsymbol symbol_title   av_sent_orig_signals  s_disagree r_disagree r_uncertainty num_receivers   shrcd  logturn* xret* 

order symbolid permno date ym ablogvol ablogvol_l1 std_orig_signals std_rec_mean_signal mean_rec_signal_std sdabret_l1to5 cabret_l1to5 cabret_l6to30 asvi media mbin0 mbin1 mbin2 mbin3 mbin4 mbin5_10 mbin11_30 mbin30p  

qui compress
save stock_day, replace 


* **************************************************************************


use stock_day, clear
drop ablogvol_f* sdev_s_disagree avg_s_disagree sdev_r_disagree avg_r_disagree num_orig_signals comnam mktcap nsignals_l5day

scalar controls ="i.media asvi sdabret_l1to5 cabret_l1to5 cabret_l6to30"  
scalar FE ="i.permno#i.ym i.date i.mbin*"  
scalar clvars = "permno date"


eststo V2: reghdfe ablogvol std_orig_signals std_rec_mean_signal ablogvol_l1 `=controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum ablogvol if e(sample)==1
	qui estadd scalar avg = r(mean), :V2

keep if e(sample)==1 


eststo V1: reghdfe ablogvol std_orig_signals ablogvol_l1 `=controls', absorb(`=FE') vce(cl `=clvars')    
	qui sum ablogvol if e(sample)==1
	qui estadd scalar avg = r(mean), :V1

eststo V3: reghdfe ablogvol std_orig_signals mean_rec_signal_std ablogvol_l1 `=controls', absorb(`=FE') vce(cl `=clvars') 
	qui sum ablogvol if e(sample)==1
	qui estadd scalar avg = r(mean), :V3

eststo V4: reghdfe ablogvol std_orig_signals std_rec_mean_signal mean_rec_signal_std ablogvol_l1 `=controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum ablogvol if e(sample)==1
	qui estadd scalar avg = r(mean), :V4

eststo V5: reghdfe ablogvol std_orig_signals std_rec_mean_signal mean_rec_signal_std ablogvol_l1, absorb(`=FE') vce(cl `=clvars')   
	sum ablogvol if e(sample)==1
	estadd scalar avg = r(mean) , :V5


********************************************


cap estadd local dfe "Y" : *
cap estadd local myfe "Y" : *
cap estadd local mbfe "Y" : *

local stats_opts " stats(N N_clust1 N_clust2 r2 avg dfe myfe mbfe, fmt(%18.0fc %18.0fc %18.0fc %12.2fc %12.2fc) labels("\# obs." "\# clusters (stock)" "\# clusters (day)" "$\textit{R}^2$" "Mean of dependent var." "Day FE" "Month x stock FE" "Message number FE") ) " 

#delimit ; 
local opts "  b(%16.3fc) se(%16.3fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
nomtitles mgroups("Abnormal log turnover\textsubscript{s,t}"
, pattern(1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;        

cd "`=tabledir'" ;
esttab V1 V2 V3 V4 V5 using T13a_volume.tex, `opts' `stats_opts'  drop(_cons 0.media) order(std_orig_signals std_rec_mean_signal mean_rec_signal_std 1.media asvi ablogvol_l1) coef(
std_orig_signals "Sender disagreement\textsubscript{s,t}" 
std_rec_mean_signal "Received disagreement\textsubscript{s,t}" 
mean_rec_signal_std "Received uncertainty\textsubscript{s,t}" 
ablogvol_l1 "Abnormal log turnover\textsubscript{s,t-1}" 
1.media "Media article\textsubscript{s,t}"
asvi "log GoogleASVI\textsubscript{s,t}"
sdabret_l1to5 "Volatility\textsubscript{s,(t-5 to t-1)}"
cabret_l1to5 "Cum. abnormal returns\textsubscript{s,(t-5 to t-1)}"
cabret_l6to30 "Cum. abnormal returns\textsubscript{s,(t-30 to t-6)}") replace  ;
#delimit cr 
cd "`=maindir'"

********************************************
********************************************
********************************************



/* Stock-Day Sumstats table (panel) */

use stock_day, clear

scalar tosumvars ="ablogvol ablogvol_l1 sdabret_l1to5 cabret_l1to5 cabret_l6to30 asvi media std_orig_signals std_rec_mean_signal mean_rec_signal_std num_orig_signals"
keep `=tosumvars' mbin* date symbolid permno ym

scalar controls ="i.media asvi sdabret_l1to5 cabret_l1to5 cabret_l6to30"  
scalar FE ="i.permno#i.ym i.date i.mbin*" 

eststo V2: reghdfe ablogvol std_orig_signals std_rec_mean_signal ablogvol_l1 `=controls', absorb(`=FE') vce(cl permno date)  
	qui sum ablogvol if e(sample)==1
	qui estadd scalar avg = r(mean), :V2

keep if e(sample)==1

local num : word count `=tosumvars'
mat sumstats = J(`num',4,.)
matrix colnames sumstats = Mean Median "Std. dev." "N obs."

cap drop row
g row=1 	
replace row=1 	
	foreach lhs in `=tosumvars' {
	qui sum `lhs' , d
	mat sumstats [row,1] = r(mean)
	mat sumstats [row,2] = r(p50)
	mat sumstats [row,3] = r(sd)
	mat sumstats [row,4] = r(N)
	qui replace row=row+1
}
mat list sumstats



local f0 "%12.0fc"
local f3 "%12.3fc"
#delimit ;
cd "`=tabledir'" ;
esttab matrix(sumstats, fmt(`f3' `f3' `f3' `f0') )  using T1c_sum_stat_panelC.tex, replace nomtitles coeflabels(
r1 "Abnormal log volume\textsubscript{s,t}" 
r2 "Abnormal log volume\textsubscript{s,t-1}" 
r3 "Std dev. abnormal returns\textsubscript{s,(t-5 to t-1)}"
r4 "Cum. abnormal returns\textsubscript{s,(t-5 to t-1)}"
r5 "Cum. abnormal returns\textsubscript{s,(t-30 to t-6)}"
r6 "Log Google ASVI\textsubscript{s,t}"
r7 "$\mathbbm{1}$ if Media article\textsubscript{s,t}"
r8 "Sender disagreement\textsubscript{s,t}" 
r9 "Received disagreement\textsubscript{s,t}" 
r10 "Received uncertainty\textsubscript{s,t}" 
r11 "Number of posts with sentiment\textsubscript{s,t}"
) ;
#delimit cr
cd "`=maindir'"



